var e, a, t = getApp();

Page({
    data: {
        tokenValue: null,
        userInfo: {},
        dialog:false,//授权弹框
        is_first_action: !0,
        userService: [ {
            name: "我的砍价",
            icon: "daojianfu",
            link: "../bargain/order/index",
            color: "#FF0C4B"
        }, {
            name: "我的拼团",
            icon: "shehuituanti",
            link: "../group/order/index",
            color: "#FFB80C"
        }, {
            name: "我的代言",
            icon: "tableshare",
            link: "../user/speak",
            color: "#00B9FD"
        }, {
            name: "增值发票",
            icon: "fapiaoguanli",
            link: "../invoice/create",
            color: "#FD4100"
        }, {
            name: "收货地址",
            icon: "dizhi",
            link: "../address/index",
            color: "#7ACF00"
        } ]
    },
    onLoad:function(){
        const me = this;
        wx.getSetting({
            success(res) {
                console.log(res.authSetting['scope.userInfo'])
                if(res.authSetting['scope.userInfo']){
                    me.setData({
                        dialog:false
                    })
                }else{
                    me.setData({
                        dialog:true
                    })
                }
           
            }
         })

        
    },
    onShow: function() {
        const value = wx.getStorageSync('token')
        if(value){
            this.setData({
                tokenValue: value
            })
        }
        var e = this;
        wx.setStorageSync("flowcheckout", {
            from: "user"
        }),
        a = wx.getStorageSync("goodshistory");
        var o = wx.getStorageSync("goodsNum");
        e.setData({
            goodsNum: o
        }),
        t.getUserInfo(function(a) {
            e.setData({
                userInfo: a
            });
        }),
        e.setData({
            is_first_action: !0
        }),
        this.setData({
            recommend: "",
            orderNum: ""
        }),
        e.user();
    },
    user: function() {
        var e = this, o = wx.getStorageSync("token");
        wx.request({
            url: t.apiUrl("user"),
            data: {
                per_page: "10",
                page: "1",
                list: a
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": o
            },
            method: "POST",
            success: function(a) {
                e.setData({
                    recommend: a.data.data.best_goods,
                    orderNum: a.data.data.order,
                    user: a.data.data,
                    hidden: !0
                });
            }
        });
    },
    bindGetUserInfo: function (e) {
        if(this.data.tokenValue){
            return
        }else{
            if (e.detail.userInfo) {
                console.log("点击了同意授权");
                this.setData({
                    userInfo: e.detail.userInfo,
                })
                var t = this;
                wx.login({
                    success: function (o) {
                        var n = o.code;
                        wx.getUserInfo({
                            withCredentials: !0,
                            lang: "zh_CN",
                            success: function (o) {
                                o.userInfo.code = n
                                t.doLogin(n, o)
                            },
                        });
                    }
                });
            }
        }
    },
    doLogin: function (e, t) {
        var o = this;
        e ? wx.request({
            url: o.apiUrl("user/login"),
            method: "POST",
            data: {
                userinfo: t,
                code: e
            },
            success: function (e) {
                o.setData({
                    dialog:false
                });
                wx.setStorage({
                    key: "token",
                    data: JSON.parse(e.data.split("\n")[1]).token
                }), wx.setStorage({
                    key: "openid",
                    data: JSON.parse(e.data.split("\n")[1]).openid
                });
                o.user()
            }
        }) : console.log("获取用户登录态失败！" + res.errMsg);
    },
    apiUrl: function (e) {
        return "https://yang.3ksy.cn/mobile/public/api/wx/" + e;
    },
    invoiceNav: function(a) {
        var o = this;
        e = wx.getStorageSync("token"), 1 == o.data.is_first_action && (o.setData({
            is_first_action: !1
        }), wx.request({
            url: t.apiUrl("user/invoice/detail"),
            method: "POST",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": e
            },
            success: function(e) {
                0 != e.data.data ? wx.navigateTo({
                    url: "../invoice/detail"
                }) : wx.navigateTo({
                    url: "../invoice/create"
                }), o.setData({
                    invoices: e.data.data
                });
            }
        }));
    },
    userAddress: function(e) {
        wx.navigateTo({
            url: "../address/index"
        });
    },
    onPullDownRefresh: function() {
        var e = this;
        wx.showNavigationBarLoading(), e.user(), setTimeout(function() {
            wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
        }, 1500);
    },
    onShareAppMessage: function() {
        return {
            title: "小程序首页",
            desc: "小程序本身无需下载，无需注册，不占用手机内存，可以跨平台使用，响应迅速，体验接近原生App",
            path: "/pages/user/user"
        };
    }
});